package org.xxpay.pay.task;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.DateUtils;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.common.vo.DBApplicationConfig;
import org.xxpay.core.entity.TransOrder;
import org.xxpay.core.service.IXxPayAgentpayService;
import org.xxpay.pay.service.CommonService;

import java.util.Date;
import java.util.List;

/**
 * @author: dingzhiwei
 * @date: 18/09/09
 * @description: 转账补单任务
 */
@Component
public class ReissueTransScheduled extends ReissuceBase {

    private static final MyLog _log = MyLog.getLog(ReissueTransScheduled.class);

    @Autowired
    private CommonService commonService;

    @Autowired
    private IXxPayAgentpayService xxPayAgentpayService;

    /**
     * 转账订单批量补单任务
     */
    @Scheduled(cron="0 0/1 * * * ?") // 每分钟执行一次
    public void transReissueTask() {
        String logPrefix = "【转账补单】";
        // 代付补单开关
        DBApplicationConfig dbConfig = commonService.sysConfigService.getDBApplicationConfig();
        if(!isExcuteReissueTask(dbConfig.getTaskReissueTransSwitch(), dbConfig.getTaskReissueTransIp())) {
            _log.info("{}当前机器不执行补单任务", logPrefix);
            return;
        }

        int pageIndex = 1, limit = 100;

        TransOrder queryCondition = new TransOrder(); queryCondition.setStatus(PayConstant.TRANS_STATUS_TRANING);
        // 查询需要处理的订单
        Date createTimeStart = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);  // 24小时
        Date createTimeEnd = new Date(System.currentTimeMillis() - 1 * 60 * 1000);          // 1分钟

        boolean flag = true;
        _log.info("{}开始查询需要处理的转账订单,查询订单{}<=创建时间<={}", logPrefix, DateUtils.getTimeStr(createTimeStart, "yyyy-MM-dd HH:mm:ss"), DateUtils.getTimeStr(createTimeEnd, "yyyy-MM-dd HH:mm:ss"));
        // 循环查询所有
        while (flag) {
            List<TransOrder> transOrderList = commonService.transOrderService.select((pageIndex - 1) * limit, limit, queryCondition, createTimeStart, createTimeEnd);
            _log.info("{}查询需要处理的转账订单,共:{}条", logPrefix, transOrderList == null ? 0 : transOrderList.size());

            for(TransOrder transOrder : transOrderList) {
                long startTime = System.currentTimeMillis(); //开始时间
                _log.info("{}开始处理transOrderId={}", logPrefix, transOrder.getTransOrderId());

                //自动查询上游接口 & 处理订单状态
                boolean isProcessSuccess = xxPayAgentpayService.queryChannelAndProcessOrderState(transOrder);

                _log.info("{}处理完毕transOrderId={}, 结果={}.耗时:{} ms", logPrefix, transOrder.getTransOrderId(),
                                                                isProcessSuccess, System.currentTimeMillis() - startTime);

            }
            pageIndex++;
            if(CollectionUtils.isEmpty(transOrderList) || transOrderList.size() < limit) {
                flag = false;
            }
        }
        _log.info("{}本次查询处理完成,", logPrefix);
    }

}
